*This file creates shift-share robustness following Borusyak et al. (2022). Table D.17 of the paper. 
*To excecute the file need to install package ssaggregate to the FIONA remote server

cd "$temp"

global municontrols   agrishare_1940   town city manushare_1940 

use bartik_50_c ,clear

joinby  mun39 using municipality_shares_bartik_1939_all_munis

collapse   bartik_standardized primary50_individual  laani50 $municontrols (sum) wei, by(mun39)

gen constant=1

destring mun39, replace
ssaggregate primary50_individual bartik [aw=wei], n(ind39) s(share) sfile(Long_shares) l(mun39) controls("$municontrols  i.laani50") 

tostring ind39, replace

joinby ind39 using "$temp\repadata_scaled.dta" 

ivreg2 primary50_individual (bartik_standardized=repa_scaled) [aw=s_n], r
estimates store e1a

estadd local sample "All"

*1950 agri back

use bartik_50_c_agri ,clear

joinby  mun39 using municipality_shares_bartik_1939_all_munis

collapse  bartik_standardized primary50_individual  laani50 $municontrols (sum) wei, by(mun39)

gen constant=1

destring mun39, replace
ssaggregate primary50_individual bartik [aw=wei], n(ind39) s(share) sfile(Long_shares) l(mun39) controls("$municontrols  i.laani50") 

tostring ind39, replace

joinby ind39 using "$temp\repadata_scaled.dta" 


ivreg2 primary50_individual (bartik_standardized=repa_scaled) [aw=s_n], r
estimates store e1b
estadd local sample "Agri 1939"

*1970 ln income
use bartik_70_c ,clear

joinby  mun39 using municipality_shares_bartik_1939_all_munis

collapse   bartik_standardized svatv  laani50 $municontrols (sum) wei, by(mun39)

gen constant=1

gen ln_svatv=ln(svatv)

destring mun39, replace
ssaggregate ln_svatv bartik [aw=wei], n(ind39) s(share) sfile(Long_shares) l(mun39) controls("$municontrols  i.laani50") 

tostring ind39, replace

joinby ind39 using "$temp\repadata_scaled.dta" 

ivreg2 ln_svatv (bartik_standardized=repa_scaled) [aw=s_n], r
estimates store e2a
estadd local sample "All"


use bartik_70_c_agri ,clear

joinby  mun39 using municipality_shares_bartik_1939_all_munis

collapse   bartik_standardized svatv  laani50 $municontrols (sum) wei, by(mun39)

gen constant=1

gen ln_svatv=ln(svatv)

destring mun39, replace
ssaggregate ln_svatv bartik [aw=wei], n(ind39) s(share) sfile(Long_shares) l(mun39) controls("$municontrols  i.laani50") 

tostring ind39, replace

joinby ind39 using "$temp\repadata_scaled.dta" 

ivreg2 ln_svatv (bartik_standardized=repa_scaled) [aw=s_n], r
estimates store e2b
estadd local age "19-40"
estadd local sample "Agri 1939"

*Income_rank

use bartik_young_UM ,clear

preserve
use municipality_shares_bartik_1939_all_munis, clear
tempfile shares
destring mun39, replace
save `shares'

restore 

destring mun39, replace

joinby  mun39 using `shares'

collapse   bartik_standardized yos income_rank laani50 $municontrols   (sum) wei, by(mun39)

gen constant=1

destring mun39, replace
ssaggregate income_rank bartik [aw=wei], n(ind39) s(share) sfile(Long_shares) l(mun39) controls("$municontrols i.laani50") 

tostring ind39, replace

joinby ind39 using "$temp\repadata_scaled.dta" 


ivreg2 income_rank (bartik_standardized=repa_scaled) [aw=s_n], r
estimates store e4b
estadd local age "Under 30"
estadd local sample "Father no EDU"


**Education  change between the groups

preserve 
use bartik_young_old ,clear
keep mun39 young hdegree yos
reshape wide hdegree yos, i(mun39) j(young)
gen diff_yos=yos1-yos0
gen diff_hdegree=hdegree1-hdegree0
tempfile change
save `change'
restore 

use bartik_young_old ,clear

joinby mun39 using  `change'

joinby  mun39 using municipality_shares_bartik_1939_all_munis

collapse  diff_hdegree diff_yos bartik_standardized yos income_rank laani50 $municontrols (sum) wei, by(mun39)

gen constant=1

destring mun39, replace
ssaggregate diff_hdegree bartik diff_yos [aw=wei], n(ind39) s(share) sfile(Long_shares) l(mun39) controls("$municontrols  i.laani50") 

tostring ind39, replace

joinby ind39 using "$temp\repadata_scaled.dta" 

ivreg2 diff_yos (bartik_standardized=repa_scaled)  [aw=s_n], r
estimates store edu1
estadd local sample "All"

ivreg2 diff_hdegree (bartik_standardized=repa_scaled)  [aw=s_n], r
estimates store edu2
estadd local sample "All"

label var bartik_standardized "Exposure"

esttab e1a e1b e2a e2b edu1 edu2 e4b ///
using "$temp\tables\TABLE_BARTIK_BHJ.tex", ///
mgroups("Agriculture 1950" "Ln income"  "Change Years of Education" "Change Higher Degrees"  "Income Rank", pattern(1 0 1 0 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) ///
span erepeat(\cmidrule(lr){@span}) ) ///
replace keep(bartik_standardized) nonotes ///
b(%9.3f) se(%9.3f)  stats(N sample, fmt(0 3) layout(@) ///
labels("N" "Sample")) label star(* .1 ** .05 *** .01) nodepvars nomtitle

